/**
 * @description 片段-控制器
 * @since 2019-11-12
 * @author Rid King
*/

const ox = require('@daelui/oxjs')
const Service = require('./service.js')

class Controller extends ox.Controller {
  /**
   * @function 构造方法
  */
  constructor (args) {
    super(args)
    // 服务
    this.$service = new Service(args)
  }

  queryPage (action) {
    // 解析数据
    return this.solveAction(action).then(async action => {
      let params = action.params || {}
      return this.$service.query({
        params: {
          id: params.id
        },
        excute: {
          operators: [
            { field: 'id', operator: 'EQUAL' }
          ]
        }
      }).then(async (res) => {
        let data = res.data || {}
        let html = data.html || ''
        // 查询路径数据
        let codes = []
        try {
          // 查询所有的分组数据
          let res = await this.$router.getControllInstance({path: '/tigerlair/dictionary'}).queryAll({}, {
            action: {},
            query: {},
            params: {
              cat: 'snippet-code',
              status: '1'
            }
          })
          let list = res.data
          list = Array.isArray(list) ? list : []
          codes = list
        } catch (e) {
          codes = []
        }
        codes.forEach(item => {
          html = html.replaceAll(item.code, item.value)
        })
        return {
          success: 1,
          data: html,
          type: 'PLAINHTML'
        }
      })
    })
  }
}

module.exports = Controller